package cn.myzqu.kafka.demo.admin;

import org.apache.kafka.clients.admin.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * kafkaAdminClient
 *
 * @author guoqiujing
 * @date 2020/11/25 15:20
 */
public class AdminClientDemo {


    private static final String brokerList = "172.168.50.171:9092";

    private static final String topic = "topic-admin";

    private AdminClientDemo() {

    }

    private static volatile AdminClient adminClient;

    public static AdminClient getInstance() {
        if (adminClient == null) {
            synchronized (AdminClient.class) {
                if (adminClient == null) {
                    Properties properties = new Properties();
                    properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
                    properties.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
                    adminClient = AdminClient.create(properties);
                }
            }
        }
        return adminClient;
    }

    public static void main(String[] args) {
//        Properties properties = new Properties();
//        properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList);
//        properties.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
//        AdminClient adminClient = AdminClient.create(properties);
        getInstance();
        //创建新主题
//        NewTopic newTopic = new NewTopic(topic, 4, (short) 1);
//        CreateTopicsResult result = adminClient.createTopics(Collections.singleton(newTopic));
//        try {
//            result.all().get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        } finally {
//            adminClient.close();
//        }
        //创建新的分区的结果
        Map<String, NewPartitions> newPartitions=new HashMap<>();
        newPartitions.put("topic-demo", NewPartitions.increaseTo(2));
        adminClient.createPartitions(newPartitions);
    }


}
